<?php
namespace app\index\relevan;

use app\index\controller\Common;
use app\index\controller\Engin;
use app\index\model\AcckuanModel;
use app\index\model\BondFanModel;
use app\index\model\BondJiaoModel;
use app\index\model\BondShouModel;
use app\index\model\BondTuiModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractBuyListModel;
use app\index\model\ContractBuyModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractLianModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\DaiKouQiListModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginGodanModel;
use app\index\model\EnginModel;
use app\index\model\EnginVisaModel;
use app\index\model\FileModel;
use app\index\model\FiletypeModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxModel;
use app\index\model\ShouHuanModel;
use app\index\model\ShouTouModel;
use app\index\model\ShouXiangKouModel;
use app\index\model\ShouXiangModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\StuffTuiModel;
use app\index\model\ZhiEnbaoModel;
use app\index\model\ZhiJieModel;
use app\index\model\ZhiJintuiListModel;
use app\index\model\ZhiJintuiModel;
use app\index\model\ZhiLianjieModel;
use app\index\model\ZhiDaybaoModel;
use app\index\model\ZhiTouModel;
use app\index\model\ZhiXiangModel;
use app\index\service\BianhaoService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\UserService;
use think\Db;
use think\Exception;
use think\facade\Config;

class EnginRelevan extends Common
{
    //更新项目管理组
    public function vate_engin($minfo){
        $upd = [];
        if($minfo['business'] == 1){
            $upd['man_zhuan'] = $this->cominfo['man_zhuan'];
            $upd['man_zi'] = $this->cominfo['man_zi'];
            $upd['man_cheng'] = $this->cominfo['man_cheng'];
            $upd['man_zhian'] = $this->cominfo['man_zhian'];
        }else{
            $upd['man_zhuan'] = $this->cominfo['man_zhuan_lian'];
            $upd['man_zi'] = $this->cominfo['man_zi_lian'];
            $upd['man_cheng'] = $this->cominfo['man_cheng_lian'];
            $upd['man_zhian'] = $this->cominfo['man_zhian_lian'];
        }
        EnginModel::where(['id'=>$minfo['id']])->update($upd);
        return true;
    }




    //预算变更 更新项目成本预算
    public function set_engin_budget_list($bianid){
        $bianInfo = EnginBudbianModel::where(['id'=>$bianid])->find();
        $budgetInfo = EnginBudgetModel::where(['id'=>$bianInfo['budgetid']])->find();

        $list = EnginBudbianListModel::where(['budbianid'=>$bianInfo['id']])->select()->toArray();
        foreach ($list as $vv){
            if($vv['listid'] > 0){
                EnginBudgetListModel::where(['id'=>$vv['listid']])->update([
                    'num' => $vv['new_num'],
                    'price' => $vv['new_price'],
                    'money' => $vv['new_money'],
                ]);
            }else{
                EnginBudgetListModel::insert([
                    'budgetid' => $budgetInfo['id'],
                    'budtype' => $vv['budtype'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }


        $upd['money'] = 0;
        $upd['caifee'] = 0;
        $upd['cairate'] = 0;
        $upd['renfee'] = 0;
        $upd['renrate'] = 0;
        $upd['jifee'] = 0;
        $upd['jirate'] = 0;
        $upd['fenfee'] = 0;
        $upd['fenrate'] = 0;
        $upd['qifee'] = 0;
        $upd['qirate'] = 0;

        $budgetList = EnginBudgetListModel::where(['budgetid'=>$budgetInfo['id']])->select()->toArray();
        foreach ($budgetList as $item){
            $upd['money'] += $item['money'];
            if($item['budtype'] == 'cai'){
                $upd['caifee'] += $item['money'];
            }
            if($item['budtype'] == 'ren'){
                $upd['renfee'] += $item['money'];
            }
            if($item['budtype'] == 'ji'){
                $upd['jifee'] += $item['money'];
            }
            if($item['budtype'] == 'fen'){
                $upd['fenfee'] += $item['money'];
            }
            if($item['budtype'] == 'qi'){
                $upd['qifee'] += $item['money'];
            }
        }
        if($upd['money'] != 0){
            $upd['cairate'] = round($upd['caifee']/$upd['money']*100, 2);
            $upd['renrate'] = round($upd['renfee']/$upd['money']*100, 2);
            $upd['jirate'] = round($upd['jifee']/$upd['money']*100, 2);
            $upd['fenrate'] = round($upd['fenfee']/$upd['money']*100, 2);
            $upd['qirate'] = round($upd['qifee']/$upd['money']*100, 2);
        }
        EnginBudgetModel::where(['id'=>$budgetInfo['id']])->update($upd);
        return true;
    }

    //更新采购合同变更明细 及其 变更后金额
    public function vate_contract_cai_list($bianid){
        $bianinfo = ContractBianModel::where(['id'=>$bianid])->find();
        $list = ContractBianListModel::where(['bianid'=>$bianinfo['id']])->select()->toArray();
        foreach($list as $vv){
            if($vv['listid'] > 0){
                ContractCaiListModel::where(['id'=>$vv['listid']])->update([
                    'listid' => $vv['bglistid'],
                    'num' => $vv['new_num'],
                    'price' => $vv['new_price'],
                    'money' => $vv['new_money'],
                ]);
            }else{
                ContractCaiListModel::insert([
                    'caiid' => $bianinfo['caiid'],
                    'listid' => $vv['bglistid'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['new_num'],
                    'price' => $vv['new_price'],
                    'money' => $vv['new_money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }

        $listnum = ContractCaiListModel::where(['caiid'=>$bianinfo['caiid']])->sum('num');
        $listmoney = ContractCaiListModel::where(['caiid'=>$bianinfo['caiid']])->sum('money');
        $bianfee = ContractBianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['enginid'=>$bianinfo['enginid'],'caiid'=>$bianinfo['caiid']])->sum('bianfee');
        ContractCaiModel::where(['id'=>$bianinfo['caiid']])->update(['listnum'=>$listnum, 'money'=>$listmoney, 'bianfee'=>$bianfee]);
        return true;
    }

    //处理进项退税明细 关联的收票明细id
    public function vate_zhi_jintui_list($jintuiid){
        $jintuiInfo = ZhiJintuiModel::where(['id'=>$jintuiid])->find();
        if($jintuiInfo['deleted_user'] == 0 && $jintuiInfo['isturn'] == 1 && $jintuiInfo['status'] == 1){
            $list = ZhiJintuiListModel::where(['zhiid'=>$jintuiid])->select()->toArray();
            foreach($list as $vv){
                PiaoShouListModel::where(['id'=>$vv['piaoid']])->update(['dikouid'=>$vv['id']]);
            }
        }else{
            $list = ZhiJintuiListModel::where(['zhiid'=>$jintuiid])->select()->toArray();
            foreach($list as $vv){
                PiaoShouListModel::where(['id'=>$vv['piaoid']])->update(['dikouid'=>0]);
            }
        }
        return true;
    }

    //材料入库 处理汇总 支出合同或者采购单 关联的入库总数
    public function vate_stuff_ru_list_and_caiorbuy($stuffruid){
        $ruInfo = StuffRuModel::where(['id'=>$stuffruid])->find();
        $ruList = StuffRuListModel::where(['ruid'=>$stuffruid])->select()->toArray();
        if($ruInfo['rutype'] == 1){
            $all_runum = 0;
            foreach ($ruList as $ruVal){
                $runum = StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0, 'ru.isturn'=>1, 'ru.status'=>1])
                    ->where(['ru.rutype'=>1, 'list.listid'=>$ruVal['listid']])
                    ->sum('list.num');
                $all_runum += $runum;

                $cailidtinfo = ContractCaiListModel::where(['id'=>$ruVal['listid']])->find();
                $shengnum = $cailidtinfo['num']-$runum;
                ContractCaiListModel::where(['id'=>$cailidtinfo['id']])->update(['runum'=>$runum,'shengnum'=>$shengnum]);
            }
            ContractCaiModel::where(['id'=>$ruInfo['caiid']])->update(['runum'=>$all_runum]);
        }
        if($ruInfo['rutype'] == 2){
            $all_runum = 0;
            foreach ($ruList as $ruVal){
                $runum = StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0, 'ru.isturn'=>1, 'ru.status'=>1])
                    ->where(['ru.rutype'=>2, 'list.listid'=>$ruVal['listid']])
                    ->sum('list.num');
                $all_runum += $runum;

                $buylidtinfo = ContractBuyListModel::where(['id'=>$ruVal['listid']])->find();
                $shengnum = $buylidtinfo['num']-$runum;
                ContractBuyListModel::where(['id'=>$buylidtinfo['id']])->update(['runum'=>$runum,'shengnum'=>$shengnum]);
            }
            ContractBuyModel::where(['id'=>$ruInfo['buyid']])->update(['runum'=>$all_runum]);
        }
        return true;
    }
    //材料入库 处理直接出库
    public function vate_stuff_ru_create_chu($stuffruid){
        $ruInfo = StuffRuModel::where(['id'=>$stuffruid])->find();
        if($ruInfo['ischu'] == '是'){
            $list = StuffRuListModel::where(['ruid'=>$ruInfo['id']])->select()->toArray();
            foreach ($list as $vvv){
                $ins = [];
                $ins['comid'] = $this->userinfo['comid'];
                $ins['chanid'] = $ruInfo['chanid'];
                $ins['deptid'] = $ruInfo['deptid'];
                $ins['enginid'] = $ruInfo['enginid'];
                $ins['ruid'] = $vvv['id'];
                $ins['number'] = (new BianhaoService())->create_stuff_chu_number();
                $ins['chudt'] = $ruInfo['rudt'];
                $ins['baner'] = $ruInfo['baner_chu'];
                $ins['num'] = $vvv['num'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                StuffChuModel::insert($ins);
            }
        }
        return true;
    }
    //材料入库 处理入库明细本身剩余库存
    public function vate_stuff_ru_list_ben($stuffruid){
        $ruInfo = StuffRuModel::where(['id'=>$stuffruid])->find();
        $list = StuffRuListModel::where(['ruid'=>$ruInfo['id']])->select()->toArray();
        foreach ($list as $vvv){
            $upd['chunum'] = StuffChuModel::where(['deleted_user'=>0, 'ruid'=>$vvv['id']])->sum('num');
            $upd['chumoney'] = $upd['chunum'] * $vvv['price'];

            $upd['tuinum'] = StuffTuiModel::where(['deleted_user'=>0, 'ruid'=>$vvv['id']])->sum('num');
            $upd['tuimoney'] = $upd['tuinum'] * $vvv['price'];

            $upd['cunnum'] = $vvv['num'] - $upd['chunum'] + $upd['tuinum'];
            $upd['cunmoney'] = $upd['cunnum'] * $vvv['price'];

            $upd['yongnum'] = $upd['chunum'] - $upd['tuinum'];
            $upd['yongmoney'] = $upd['yongnum'] * $vvv['price'];
            StuffRuListModel::where(['id'=>$vvv['id']])->update($upd);
        }
        return true;
    }
    //出库 退库 处理入库明细本身剩余库存
    public function vate_stuff_chutui($rulistid){
        $rulistinfo = StuffRuListModel::where(['id'=>$rulistid])->find();

        $upd['chunum'] = StuffChuModel::where(['deleted_user'=>0, 'ruid'=>$rulistinfo['id']])->sum('num');
        $upd['chumoney'] = $upd['chunum'] * $rulistinfo['price'];

        $upd['tuinum'] = StuffTuiModel::where(['deleted_user'=>0, 'ruid'=>$rulistinfo['id']])->sum('num');
        $upd['tuimoney'] = $upd['tuinum'] * $rulistinfo['price'];

        $upd['cunnum'] = $rulistinfo['num'] - $upd['chunum'] + $upd['tuinum'];
        $upd['cunmoney'] = $upd['cunnum'] * $rulistinfo['price'];

        $upd['yongnum'] = $upd['chunum'] - $upd['tuinum'];
        $upd['yongmoney'] = $upd['yongnum'] * $rulistinfo['price'];
        StuffRuListModel::where(['id'=>$rulistinfo['id']])->update($upd);
        return true;
    }
    //处理工程量清单信息
    public function vate_engin_godan_list($godanid){
        $godanInfo = EnginGodanModel::where(['id'=>$godanid])->find();

        $existGodanid = 0;
        $exist = EnginGodanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$godanInfo['enginid']])->find();
        if($exist){
            $existGodanid = $exist['id'];
        }

        EnginModel::where(['id'=>$godanInfo['enginid']])->update(['godanid'=>$existGodanid]);
        return true;
    }
    //备用金还款 - 关联的借支信息余额等
    public function vate_shou_huan_info($mid){
        $info = ShouHuanModel::where(['id'=>$mid])->find();
        $this->vate_zhi_jie_info($info['jieid']);
        return true;
    }
    //项目费用报销 - 关联的借支信息余额等
    public function vate_zhi_enbao_info($mid){
        $info = ZhiEnbaoModel::where(['id'=>$mid])->find();
        if($info['isdi'] == '是' && $info['jieid'] > 0){
            $this->vate_zhi_jie_info($info['jieid']);
        }
        return true;
    }
    //日常费用报销 - 关联的借支信息余额等
    public function vate_zhi_daybao_info($mid){
        $info = ZhiDaybaoModel::where(['id'=>$mid])->find();
        if($info['isdi'] == '是' && $info['jieid'] > 0){
            $this->vate_zhi_jie_info($info['jieid']);
        }
        return true;
    }
    //处理借支信息余额
    public function vate_zhi_jie_info($zhijieid){
        $zhijieinfo = ZhiJieModel::where(['id'=>$zhijieid])->find();
        $yizhifee = (new FinanceService())->get_payfee_by_mid(26, $zhijieinfo['id']);
        $huanfee = ShouHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$zhijieinfo['id']])->sum('money');
        $difee = ZhiEnbaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$zhijieinfo['id']])->sum('difee');
        $difee += ZhiDaybaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$zhijieinfo['id']])->sum('difee');
        $yufee = $yizhifee - $huanfee - $difee;

        ZhiJieModel::where(['id'=>$zhijieinfo['id']])->update([
            'yizhifee'=>$yizhifee,
            'huanfee'=>$huanfee,
            'difee'=>$difee,
            'yufee'=>$yufee,
        ]);

        $this->set_zhi_jie($zhijieinfo);
        return true;
    }

    //处理交易信息回调数据
    public function set_account_list_vateinfo($kuanid){
        $kuanInfo = AcckuanModel::where(['id'=>$kuanid])->find();

        if($kuanInfo['modtype'] == 26){
            $this->vate_zhi_jie_info($kuanInfo['mid']);
        }

        return true;
    }

    //保证金缴纳
    public function vate_bond_jiao_info($mid){
        $info = BondJiaoModel::where(['id'=>$mid])->find();
        $yituifee = BondTuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jiaoid'=>$info['id']])->sum('money');
        $yikoufee = BondTuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jiaoid'=>$info['id']])->sum('koufee');

        $daituifee = $info['money'] - $yituifee - $yikoufee;
        BondJiaoModel::where(['id'=>$info['id']])->update([
            'yituifee'=>$yituifee,
            'yikoufee'=>$yikoufee,
            'daituifee'=>$daituifee,
        ]);

        if($info['isshou'] == '是' && $info['shouid'] > 0){
            $this->vate_bond_shou_info($info['shouid']);
        }
        return true;
    }
    //保证金收取
    public function vate_bond_shou_info($mid){
        $info = BondShouModel::where(['id'=>$mid])->find();
        if(!$mid || !$info){
            return true;
        }
        //本收取下的缴纳合计金额    本收取下的退回到账金额
        $jiaoidArr = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['isshou'=>'是','shouid'=>$info['id']])->column('id');

        $yijiaofee = BondJiaoModel::where('id','in', $jiaoidArr)->sum('money');
        $yituifee = BondTuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where('jiaoid','in', $jiaoidArr)->sum('money');
        $yituikoufee = BondTuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where('jiaoid','in', $jiaoidArr)->sum('koufee');
        $yifanfee = BondFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['shouid'=>$info['id']])->sum('money');
        $kefanfee = $info['money'] - $yijiaofee + $yituifee - $yifanfee;


        BondShouModel::where(['id'=>$info['id']])->update([
            'yijiaofee'=>$yijiaofee,
            'yituifee'=>$yituifee,
            'yituikoufee'=>$yituikoufee,
            'yifanfee'=>$yifanfee,
            'kefanfee'=>$kefanfee,
        ]);
        return true;
    }
    public function vate_bond_info($enginid){
        $enginInfo = EnginModel::where(['id'=>$enginid])->find();

        //保证金相关信息
        $bondshoufee = BondShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');
        $bondjiaofee = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');
        $bondtuifee = BondTuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');
        $bondfanfee = BondFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');

        $baoyufee = round($bondshoufee - $bondjiaofee + $bondtuifee - $bondfanfee, 2);
        $biaoqianfunfee = $enginInfo['shoutoufee'] - $enginInfo['zhitoufee'] + $baoyufee;
        EnginModel::where(['id'=>$enginid])->update([
            'bondshoufee'=>$bondshoufee,
            'bondjiaofee'=>$bondjiaofee,
            'bondtuifee'=>$bondtuifee,
            'bondfanfee'=>$bondfanfee,
            'baoyufee'=>$baoyufee,
            'biaoqianfunfee'=>$biaoqianfunfee,
        ]);
        return true;
    }


    public function set_contract_shi($minfo){
        $shimoney = 0;
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$minfo['enginid']])->find();
        if($shiinfo){
            $bu_money = ContractBuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$minfo['enginid'],'shiid'=>$shiinfo['id']])->sum('money');
            $shimoney = $shiinfo['money'] + $bu_money;
        }
        EnginModel::where(['id'=>$minfo['enginid']])->update([
            'shimoney'=>$shimoney,
        ]);
        return true;
    }
    public function set_contract_cai($data){
        $caimoney = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid']])->sum('money');
        $cai_hefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'cai'])->sum('money');
        $ren_hefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'ren'])->sum('money');
        $ji_hefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'ji'])->sum('money');
        $fen_hefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'fen'])->sum('money');
        $qi_hefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'qi'])->sum('money');
        $caijiemoney = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid']])->sum('caijiemoney');
        $cai_jiefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'cai'])->sum('caijiemoney');
        $ren_jiefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'ren'])->sum('caijiemoney');
        $ji_jiefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'ji'])->sum('caijiemoney');
        $fen_jiefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'fen'])->sum('caijiemoney');
        $qi_jiefee = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['enginid'],'caitype'=>'qi'])->sum('caijiemoney');

        EnginModel::where(['id'=>$data['enginid']])->update([
            'caimoney'=>$caimoney,
            'cai_hefee'=>$cai_hefee,
            'ren_hefee'=>$ren_hefee,
            'ji_hefee'=>$ji_hefee,
            'fen_hefee'=>$fen_hefee,
            'qi_hefee'=>$qi_hefee,
            'caijiemoney'=>$caijiemoney,

            'cai_jiefee'=>$cai_jiefee,
            'ren_jiefee'=>$ren_jiefee,
            'ji_jiefee'=>$ji_jiefee,
            'fen_jiefee'=>$fen_jiefee,
            'qi_jiefee'=>$qi_jiefee,
        ]);
        (new EnginRelevan())->set_engin_budget($data['enginid']);
        return true;
    }
    public function set_contract_bu($minfo){
        $shiinfo = ContractShiModel::where(['id'=>$minfo['shiid']])->find();
        $bu_money = ContractBuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$minfo['enginid'],'shiid'=>$minfo['shiid']])->sum('money');
        ContractShiModel::where(['id'=>$minfo['shiid']])->update([
            'bumoney'=>$bu_money,
        ]);
        $this->set_contract_shi($shiinfo);
        return true;
    }
    public function set_contract_shi_jie($mid){
        $info = ContractShiJieModel::where(['id'=>$mid])->find();
        if(!$mid || !$info){
            return true;
        }
        ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$info['enginid'],'shiid'=>$info['shiid']])->update(['real'=>0]);
        $shi_zui = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'jietype'=>'最终结算'])->find();
        if($shi_zui){
            ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'jietype'=>'最终结算'])->update(['real'=>1]);
        }else{
            ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'jietype'=>'进度结算'])->update(['real'=>1]);
        }

        $data['shijiejinmoney'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'jietype'=>'进度结算'])->sum('money');
        $data['shijiemoney'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'real'=>1])->sum('money');
        $data['shijiekoufafee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'real'=>1])->sum('koufafee');
        $data['shijiejingfee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'shiid'=>$info['shiid'],'real'=>1])->sum('jingfee');
        EnginModel::where(['id'=>$info['enginid']])->update([
            'shijiejinmoney' => $data['shijiejinmoney'],
            'shijiemoney' => $data['shijiemoney'],
            'shijiekoufafee' => $data['shijiekoufafee'],
            'shijiejingfee' => $data['shijiejingfee'],
        ]);
        return true;
    }
    public function set_contract_cai_jie($mid){
        $info = ContractCaiJieModel::where(['id'=>$mid])->find();
        if(!$mid || !$info){
            return true;
        }
        $caiinfo = ContractCaiModel::where(['id'=>$info['caiid']])->find();

        ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$info['enginid'],'caiid'=>$info['caiid']])->update(['real'=>0]);
        $cai_zui = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'jietype'=>'最终结算'])->find();
        if($cai_zui){
            ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'jietype'=>'最终结算'])->update(['real'=>1]);
        }else{
            ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'jietype'=>'进度结算'])->update(['real'=>1]);
        }

        $data['caijiejinmoney'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'jietype'=>'进度结算'])->sum('money');
        $data['caijiemoney'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'real'=>1])->sum('money');
        $data['caijiekoufafee'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'real'=>1])->sum('koufafee');
        $data['caijiejingfee'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$info['enginid'],'caiid'=>$info['caiid'],'real'=>1])->sum('jingfee');
        ContractCaiModel::where(['id'=>$info['caiid']])->update([
            'caijiejinmoney' => $data['caijiejinmoney'],
            'caijiemoney' => $data['caijiemoney'],
            'caijiekoufafee' => $data['caijiekoufafee'],
            'caijiejingfee' => $data['caijiejingfee'],
        ]);

        $this->set_contract_cai($caiinfo);
        return true;
    }
    public function set_engin_budget($enginid){
        $enginInfo = EnginModel::where(['id'=>$enginid])->find();
        $budgetInfo = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$enginid])->find();
        if(!$budgetInfo){
            $budgetInfo['money'] = 0;
            $budgetInfo['caifee'] = 0;
            $budgetInfo['renfee'] = 0;
            $budgetInfo['jifee'] = 0;
            $budgetInfo['fenfee'] = 0;
            $budgetInfo['qifee'] = 0;
        }

        $data['budget_money'] = $budgetInfo['money'];
        $data['budget_caifee'] = $budgetInfo['caifee'];
        $data['budget_renfee'] = $budgetInfo['renfee'];
        $data['budget_jifee'] = $budgetInfo['jifee'];
        $data['budget_fenfee'] = $budgetInfo['fenfee'];
        $data['budget_qifee'] = $budgetInfo['qifee'];

        $data['budget_rate_all'] = 0;
        $data['budget_rate_cai'] = 0;
        $data['budget_rate_ren'] = 0;
        $data['budget_rate_ji'] = 0;
        $data['budget_rate_fen'] = 0;
        $data['budget_rate_qi'] = 0;

        if($data['budget_money'] != 0){
            $data['budget_rate_all'] = $enginInfo['caimoney'] / $data['budget_money'] * 100;
        }
        if($data['budget_caifee'] != 0){
            $data['budget_rate_cai'] = $enginInfo['cai_hefee'] / $data['budget_caifee'] * 100;
        }
        if($data['budget_renfee'] != 0){
            $data['budget_rate_ren'] = $enginInfo['ren_hefee'] / $data['budget_renfee'] * 100;
        }
        if($data['budget_jifee'] != 0){
            $data['budget_rate_ji'] = $enginInfo['ji_hefee'] / $data['budget_jifee'] * 100;
        }
        if($data['budget_fenfee'] != 0){
            $data['budget_rate_fen'] = $enginInfo['fen_hefee'] / $data['budget_fenfee'] * 100;
        }
        if($data['budget_qifee'] != 0){
            $data['budget_rate_qi'] = $enginInfo['qi_hefee'] / $data['budget_qifee'] * 100;
        }

        $data['budget_zhi_all'] = $data['budget_money'] - $enginInfo['caimoney'];
        $data['budget_zhi_cai'] = $data['budget_caifee'] - $enginInfo['cai_hefee'];
        $data['budget_zhi_ren'] = $data['budget_renfee'] - $enginInfo['ren_hefee'];
        $data['budget_zhi_ji'] = $data['budget_jifee'] - $enginInfo['ji_hefee'];
        $data['budget_zhi_fen'] = $data['budget_fenfee'] - $enginInfo['fen_hefee'];
        $data['budget_zhi_qi'] = $data['budget_qifee'] - $enginInfo['qi_hefee'];

        EnginModel::where(['id'=>$enginInfo['id']])->update($data);
        return true;
    }
    public function set_contract_buy($data){
        $buymoney = ContractBuyModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update(['buymoney'=>$buymoney]);
        return true;
    }
    public function set_engin_visa($data){
        $visamoney = EnginVisaModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update(['visamoney'=>$visamoney]);
        return true;
    }
    public function set_engin_chan($data){
        $chanmoney = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update(['chanmoney'=>$chanmoney]);
        return true;
    }
    public function set_piao_kai($data){
        //项目开票金额 、税额、不含税金额   【审核通过】主信息字段
        $kai_money = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $kai_ratefee = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('ratefee');

        $kai_nofee = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('nofee');

        EnginModel::where(['id'=>$data['enginid']])->update([
            'kai_money'=>$kai_money,
            'kai_ratefee'=>$kai_ratefee,
            'kai_nofee'=>$kai_nofee,
        ]);
        $this->set_piao_shou($data);
        return true;
    }
    public function set_piao_shou($data){
        $enginInfo = EnginModel::where(['id'=>$data['enginid']])->find();

        //项目收票金额 、税额、不含税金额   【审核通过】主信息字段shou
        $shou_money = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $shou_ratefee = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('ratefee');

        $shou_ratefee_zhuan = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('ratefee_zhuan');

        $shou_nofee = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('nofee');

        $zengzhifee = $enginInfo['kai_ratefee'] - $shou_ratefee_zhuan;

        $shou_money_zhuan =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.piaotype'=>1])
            ->sum('list.money');

        $shou_money_pu =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.piaotype'=>2])
            ->sum('list.money');

        $shou_money_cai =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.yongtype'=>'cai'])
            ->sum('list.money');

        $shou_money_ren =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.yongtype'=>'ren'])
            ->sum('list.money');

        $shou_money_ji =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.yongtype'=>'ji'])
            ->sum('list.money');

        $shou_money_fen =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.yongtype'=>'fen'])
            ->sum('list.money');

        $shou_money_qi =  PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['enginid'],'list.yongtype'=>'qi'])
            ->sum('list.money');



        EnginModel::where(['id'=>$data['enginid']])->update([
            'shou_money'=>$shou_money,
            'shou_ratefee'=>$shou_ratefee,
            'shou_ratefee_zhuan'=>$shou_ratefee_zhuan,
            'shou_nofee'=>$shou_nofee,
            'zengzhifee'=>$zengzhifee,

            'shou_money_zhuan'=>$shou_money_zhuan,
            'shou_money_pu'=>$shou_money_pu,

            'shou_money_cai'=>$shou_money_cai,
            'shou_money_ren'=>$shou_money_ren,
            'shou_money_ji'=>$shou_money_ji,
            'shou_money_fen'=>$shou_money_fen,
            'shou_money_qi'=>$shou_money_qi,
        ]);
        return true;
    }
    public function set_piao_tax($data){
        $piaotaxfee = PiaoTaxModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update([
            'piaotaxfee'=>$piaotaxfee,
        ]);
        return true;
    }
    public function set_shou_tou($data){
        $shoutoufee = ShouTouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $zhitoufee = ZhiTouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $toushouzhicha = $shoutoufee - $zhitoufee;

        EnginModel::where(['id'=>$data['enginid']])->update([
            'shoutoufee'=>$shoutoufee,
            'zhitoufee'=>$zhitoufee,
            'toushouzhicha'=>$toushouzhicha,
        ]);

        $this->vate_bond_info($data['enginid']);
        return true;
    }


    public function set_engin_daikouqi_info($enginid){
        $daikouqifee = DaiKouQiListModel::alias('list')
            ->join('dai_kou_qi dai','dai.id=list.kouqiid')
            ->where(['dai.comid'=>$this->userinfo['comid'],'dai.deleted_user'=>0])
            ->where(['dai.isturn'=>1,'dai.status'=>1])
            ->where(['dai.enginid'=>$enginid])
            ->sum('list.money');

        $daikouqikoufee = DaiKouQiListModel::alias('list')
            ->join('dai_kou_qi dai','dai.id=list.kouqiid')
            ->where(['dai.comid'=>$this->userinfo['comid'],'dai.deleted_user'=>0])
            ->where(['dai.isturn'=>1,'dai.status'=>1])
            ->where(['dai.enginid'=>$enginid])
            ->where('list.kouid','>', 0)
            ->sum('list.money');

        $daikouqidaifee = DaiKouQiListModel::alias('list')
            ->join('dai_kou_qi dai','dai.id=list.kouqiid')
            ->where(['dai.comid'=>$this->userinfo['comid'],'dai.deleted_user'=>0])
            ->where(['dai.isturn'=>1,'dai.status'=>1])
            ->where(['dai.enginid'=>$enginid])
            ->where('list.kouid','<', 1)
            ->sum('list.money');

        EnginModel::where(['id'=>$enginid])->update([
            'daikouqifee' => $daikouqifee,
            'daikouqikoufee' => $daikouqikoufee,
            'daikouqidaifee' => $daikouqidaifee,
        ]);
    }
    public function set_shou_xiang_kou_list($data){
        $info = ShouXiangModel::where(['id'=>$data['id']])->find();
        $list = ShouXiangKouModel::where(['shouid'=>$info['id']])->select()->toArray();
        if($info['deleted_user'] == 0 && $info['isturn'] == 1 && $info['status'] == 1) {
            foreach ($list as $vvv){
                DaiKouQiListModel::where(['id'=>$vvv['daikouqiid']])->update(['kouid'=>$vvv['id']]);
            }
        }else{
            foreach ($list as $vvv){
                DaiKouQiListModel::where(['id'=>$vvv['daikouqiid']])->update(['kouid'=>0]);
            }
        }
        $this->set_engin_daikouqi_info($info['enginid']);
        return true;
    }
    public function set_shou_xiang($data){
        $shouxiangfee = ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        //工程回款金额
        $xmsr_gchk = (new UserService())->get_optionfeiid_bynum('xmsr_gchk');
        $shougonghuifee = ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmsr_gchk])->sum('money');
        //联营方备用金
        $xmsr_lyfbyj = (new UserService())->get_optionfeiid_bynum('xmsr_lyfbyj');
        $shoulianbeifee = ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmsr_lyfbyj])->sum('money');
        //公司垫资
        $xmsr_gsdz = (new UserService())->get_optionfeiid_bynum('xmsr_gsdz');
        $shougongdianfee = ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmsr_gsdz])->sum('money');

        EnginModel::where(['id'=>$data['enginid']])->update([
            'shouxiangfee'=>$shouxiangfee,
            'shougonghuifee'=>$shougonghuifee,
            'shoulianbeifee'=>$shoulianbeifee,
            'shougongdianfee'=>$shougongdianfee,
        ]);
        return true;
    }
    public function set_zhi_tou($data){
        $shoutoufee = ShouTouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $zhitoufee = ZhiTouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $toushouzhicha = $shoutoufee - $zhitoufee;

        EnginModel::where(['id'=>$data['enginid']])->update([
            'shoutoufee'=>$shoutoufee,
            'zhitoufee'=>$zhitoufee,
            'toushouzhicha'=>$toushouzhicha,
        ]);
        $this->vate_bond_info($data['enginid']);
        return true;
    }
    public function set_zhi_xiang($data){
        $zhixiangfee = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $xmzc_clf = (new UserService())->get_optionfeiid_bynum('xmzc_clf');
        $xmzc_rgf = (new UserService())->get_optionfeiid_bynum('xmzc_rgf');
        $xmzc_jxsbf = (new UserService())->get_optionfeiid_bynum('xmzc_jxsbf');
        $xmzc_zyfbf = (new UserService())->get_optionfeiid_bynum('xmzc_zyfbf');
        $xmzc_qtf = (new UserService())->get_optionfeiid_bynum('xmzc_qtf');

        $zhixiang_cai = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmzc_clf])->sum('money');

        $zhixiang_ren = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmzc_rgf])->sum('money');

        $zhixiang_ji = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmzc_jxsbf])->sum('money');

        $zhixiang_fen = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmzc_zyfbf])->sum('money');

        $zhixiang_qi = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid'],'feilei'=>$xmzc_qtf])->sum('money');

        EnginModel::where(['id'=>$data['enginid']])->update([
            'zhixiangfee'=>$zhixiangfee,
            'zhixiang_cai'=>$zhixiang_cai,
            'zhixiang_ren'=>$zhixiang_ren,
            'zhixiang_ji'=>$zhixiang_ji,
            'zhixiang_fen'=>$zhixiang_fen,
            'zhixiang_qi'=>$zhixiang_qi,
        ]);
        return true;
    }
    public function set_zhi_enbao($data){
        $zhienbaofee = ZhiEnbaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update([
            'zhienbaofee'=>$zhienbaofee,
        ]);
        return true;
    }
    public function set_zhi_jintui($data){
        $zhijintuifee = ZhiJintuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update([
            'zhijintuifee'=>$zhijintuifee,
        ]);
        return true;
    }


    public function set_zhi_lianjie_kou_list($data){
        $info = ZhiLianjieModel::where(['id'=>$data['id']])->find();
        $list = ShouXiangKouModel::where(['zhiid'=>$info['id']])->select()->toArray();
        if($info['deleted_user'] == 0 && $info['isturn'] == 1 && $info['status'] == 1) {
            foreach ($list as $vvv){
                DaiKouQiListModel::where(['id'=>$vvv['daikouqiid']])->update(['kouid'=>$vvv['id']]);
            }
        }else{
            foreach ($list as $vvv){
                DaiKouQiListModel::where(['id'=>$vvv['daikouqiid']])->update(['kouid'=>0]);
            }
        }
        $this->set_engin_daikouqi_info($info['enginid']);
        return true;
    }
    public function set_zhi_lianjie($data){
        $zhilianjiefee = ZhiLianjieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');
        EnginModel::where(['id'=>$data['enginid']])->update([
            'zhilianjiefee'=>$zhilianjiefee,
        ]);
        return true;
    }
    public function set_zhi_jie($data){
        $zhijiefee = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('money');

        $zhijiehuanfee = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('huanfee');

        $zhijiedifee = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('difee');

        $zhijieyufee = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('yufee');

        EnginModel::where(['id'=>$data['enginid']])->update([
            'zhijiefee'=>$zhijiefee,
            'zhijiehuanfee'=>$zhijiehuanfee,
            'zhijiedifee'=>$zhijiedifee,
            'zhijieyufee'=>$zhijieyufee,
        ]);
        return true;
    }
    public function set_contract_lian($data){
        //联营合同相关信息【审核通过】
        //管理费=工程回款金额×管理费费率/100
        //税费金额=开具销项票价税合计金额×税费费率/100
        $lian_lian_unit = '';
        $lian_guanrate = 0;
        $lian_shuirate = 0;
        $lianinfo = ContractLianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->find();
        if($lianinfo){
            $lian_lian_unit = $lianinfo['lian_unit'];
            $lian_guanrate = $lianinfo['guanrate'];
            $lian_shuirate = $lianinfo['shuirate'];
        }
        EnginModel::where(['id'=>$data['enginid']])->update([
            'lian_lian_unit'=>$lian_lian_unit,
            'lian_guanrate'=>$lian_guanrate,
            'lian_shuirate'=>$lian_shuirate,
        ]);
        return true;
    }
    public function set_shouzhi_kou_info($data){
        $enginInfo = EnginModel::where(['id'=>$data['enginid']])->find();

        //管理费=工程回款金额×管理费费率/100
        $lian_guanfee = $enginInfo['shougonghuifee'] * $enginInfo['lian_guanrate'] / 100;
        //税费金额=开具销项票价税合计金额×税费费率/100
        $lian_shuifee = $enginInfo['kai_money'] * $enginInfo['lian_shuirate'] / 100;

        //项目已扣收全部费用   全部项目收款扣收金额+全部联营结算扣收金额
        //项目已扣收管理费   扣收明细【项目收款和联营结算】 费用项=【管理费】
        //项目已扣收税费   扣收明细【项目收款和联营结算】 费用项=【税费】
        //项目已扣收其他费   全部费用 - 管理费 - 税费
        $kou_allfee = 0;
        $kou_guanfee = 0;
        $kou_shuifee = 0;
        $kou_qifee = 0;

        $kou_allfee += ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('koufee');

        $kou_guanfee += ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('kouguanfee');

        $kou_shuifee += ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('koushuifee');

        $kou_qifee += ShouXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('kouqifee');

        $kou_allfee += ZhiLianjieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('koufee');

        $kou_guanfee += ZhiLianjieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('kouguanfee');

        $kou_shuifee += ZhiLianjieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('koushuifee');

        $kou_qifee += ZhiLianjieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['enginid']])->sum('kouqifee');

        //待扣管理费、待扣税费   【应扣-已扣】
        $dai_guanfee  = $lian_guanfee - $kou_guanfee;
        $dai_shuifee  = $lian_shuifee - $kou_shuifee;


        //剩余可退进项税额  当累计进项票(专票)税额＞已扣收税费时，=已扣收税费-累计已退进项税额；反之累计进项票(专票)税额-累计已退进项税额
        $shengjintuifee  = 0;
        if($enginInfo['shou_ratefee_zhuan'] > $kou_shuifee){
            $shengjintuifee = $kou_shuifee - $enginInfo['zhijintuifee'];
        }else{
            $shengjintuifee = $enginInfo['shou_ratefee_zhuan'] - $enginInfo['zhijintuifee'];
        }

        EnginModel::where(['id'=>$data['enginid']])->update([
            'lian_guanfee'=>$lian_guanfee,
            'lian_shuifee'=>$lian_shuifee,

            'kou_allfee'=>$kou_allfee,
            'kou_guanfee'=>$kou_guanfee,
            'kou_shuifee'=>$kou_shuifee,
            'kou_qifee'=>$kou_qifee,

            'dai_guanfee'=>$dai_guanfee,
            'dai_shuifee'=>$dai_shuifee,

            'shengjintuifee'=>$shengjintuifee,
        ]);
        return true;
    }



    //更新项目相关信息
    public function set_engin_vateinfo($enginid){
        $enginInfo = EnginModel::where(['id'=>$enginid])->find();

        $whereCom[] = ['comid','=',$this->userinfo['comid']];
        $whereCom[] = ['deleted_user','=', 0];

        $whereTurn[] = ['isturn','=',1];
        $whereTurn[] = ['status','=',1];

        //项目支出合同结算金额  【审核通过的合同下】有最终结算的优先，否则合计进度结算金额
        $caiinfo = ContractCaiModel::where($whereCom)->where($whereTurn)->where(['enginid'=>$enginid])->select()->toArray();
        foreach ($caiinfo as $caivv){
            $caihe_zhixiangfee = ZhiXiangModel::where($whereCom)->where($whereTurn)->where(['enginid'=>$enginid,'caiid'=>$caivv['id']])->sum('money');
            $caihe_piaoshoufee = PiaoShouListModel::alias('list')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
                ->where(['shou.isturn'=>1,'shou.status'=>1])
                ->where(['shou.enginid'=>$enginid,'list.caiid'=>$caivv['id']])
                ->sum('list.money');
            $caihe_jiedaifee = $caivv['caijiemoney'] - $caihe_zhixiangfee;
            $caihe_stuffrufee = StuffRuModel::where($whereCom)->where($whereTurn)->where(['enginid'=>$enginid,'rutype'=>1,'caiid'=>$caivv['id']])->sum('money');
            $daifufee_anhe = $caivv['money'] - $caihe_zhixiangfee;
            $daifufee_anjie = $caivv['caijiemoney'] - $caihe_zhixiangfee;
            ContractCaiModel::where(['id'=>$caivv['id']])->update([
                'zhixiangfee'=>$caihe_zhixiangfee,
                'piaoshoufee'=>$caihe_piaoshoufee,
                'jiedaifee'=>$caihe_jiedaifee,
                'stuffrufee'=>$caihe_stuffrufee,
                'daifufee_anhe'=>$daifufee_anhe,
                'daifufee_anjie'=>$daifufee_anjie,
            ]);
        }



        //项目余额
        //自营：收入【工程回款金额+公司垫资】  - 支出【全部项目付款+全部项目费用报销】
        //联营：收入【工程回款金额+联营方备用金】  - 支出【全部项目付款+全部进项退税+全部联营结算】 - 扣收【全部项目收款扣收金额+全部联营结算扣收金额】
        $data['ziyingshoufee'] = 0;
        $data['lianyingshoufee'] = 0;
        $data['ziyingzhifee'] = 0;
        $data['lianyingzhifee'] = 0;
        $data['balancefee'] = 0;
        if($enginInfo['business'] == 1){
            $data['ziyingshoufee'] = $enginInfo['shougonghuifee']+$enginInfo['shougongdianfee'];
            $data['ziyingzhifee'] = $enginInfo['zhixiangfee']+$enginInfo['zhienbaofee'];
            $data['balancefee'] = $data['ziyingshoufee'] - $data['ziyingzhifee'];
        }else{
            $data['lianyingshoufee'] = $enginInfo['shougonghuifee']+$enginInfo['shoulianbeifee'];
            $data['lianyingzhifee'] = $enginInfo['zhixiangfee']+$enginInfo['zhilianjiefee'];
            $data['balancefee'] = $data['lianyingshoufee'] - $data['lianyingzhifee'] - $enginInfo['kou_allfee'];
        }

        //待回款按合同
        //待回款按开票
        //待回款按结算
        $data['daihuifee_anhe'] = $enginInfo['shimoney'] - $enginInfo['shougonghuifee'];
        $data['daihuifee_ankai'] = $enginInfo['kai_money'] - $enginInfo['shougonghuifee'];
        $data['daihuifee_anjie'] = $enginInfo['shijiemoney'] - $enginInfo['shougonghuifee'];

        EnginModel::where(['id'=>$enginid])->update([
            'ziyingshoufee' => $data['ziyingshoufee'],
            'lianyingshoufee' => $data['lianyingshoufee'],
            'ziyingzhifee' => $data['ziyingzhifee'],
            'lianyingzhifee' => $data['lianyingzhifee'],
            'balancefee' => $data['balancefee'],

            'daihuifee_anhe' => $data['daihuifee_anhe'],
            'daihuifee_ankai' => $data['daihuifee_ankai'],
            'daihuifee_anjie' => $data['daihuifee_anjie'],
        ]);
        return true;
    }







    public function vate_engin_file($table, $mid){
        $minfo = Db::name($table)->where(['id'=>$mid])->find();
        if(!$minfo){
            return true;
        }
        if($table == 'engin'){
            $minfo['enginid'] = $minfo['id'];
        }
        if(!isset($minfo['enginid']) || !$minfo['enginid'] || $minfo['enginid'] == 0){
            return true;
        }

        $tablename = '';
        $tableArr = (new EnginService())->get_engin_fold_table();
        foreach ($tableArr as $mnum=>$mname){
            if($table == $mnum){
                $tablename = $mname;
            }
        }

        if(!$tablename){
            return true;
        }

        if(isset($minfo['isturn']) && isset($minfo['status'])){
            if($minfo['isturn'] != 1 || $minfo['status'] != 1){
                $minfo['deleted_user'] = 99;
            }
        }




        EnginFoldModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$minfo['enginid'],'table'=>$table,'mid'=>$minfo['id']])->delete();
        if($minfo['deleted_user'] > 0){
            return true;
        }

        $menuFiles = FileModel::where(['table'=>$table,'mid'=>$minfo['id']])->select()->toArray();


        foreach ($menuFiles as $vvv){
            $typeidname = FiletypeModel::where(['id'=>$vvv['typeid']])->value('name');
            $typeidname = $typeidname ? $typeidname : FiletypeModel::DEATYPENAME;

            $ins = [];
            $ins['comid'] = $this->userinfo['comid'];
            $ins['enginid'] = $minfo['enginid'];
            $ins['typename'] = $typeidname;
            $ins['table'] = $table;
            $ins['tablename'] = $tablename;
            $ins['mid'] = $minfo['id'];
            $ins['name'] = $vvv['name'];
            $ins['ext'] = $vvv['ext'];
            $ins['size'] = $vvv['size'];
            $ins['oripath'] = $vvv['oripath'];
            $ins['created_user'] = $minfo['created_user'];
            $ins['created_at'] = $minfo['created_at'];

            $ins_id = EnginFoldModel::insertGetId($ins);
            $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
            if(EnginFoldModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                throw new Exception("项目相关附件处理失败");
            }
        }

        return true;
    }
















}
